package com.itheima.stock.config;

import com.itheima.stock.common.domain.StockInfoConfig;
import com.itheima.stock.utils.IdWorker;
import com.itheima.stock.utils.ParserStockInfoUtil;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 定义公共的配置bean
 */
@Configuration
@EnableConfigurationProperties(StockInfoConfig.class)
public class CommonConfig {


    /**
     * 定义密码加密和解密
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){

        return new BCryptPasswordEncoder();
    }

    //配置id生成器，保证分布式环境下id唯一
    @Bean
    public IdWorker idWorker(){
        //(机器id，指定机房的id序列号)
        IdWorker idWorker = new IdWorker(1L, 2L);
        return idWorker;
    }

    @Bean
    public ParserStockInfoUtil parserStockInfoUtil(IdWorker idWorker){
        return new ParserStockInfoUtil(idWorker);
    }
}
